package com.changanw.twonumbersum;


import java.util.HashMap;
import java.util.Map;

public class TwoNumSum {
    public int[] twoSum(int[] nums, int target) {
        //创建一个map，key是数组的第i个值，value是 i。
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            //如果存在 目标值-当前值的 key，直接返回当前值的索引i 和目标值减去当前值的索引
            if(map.containsKey(target - nums[i])) {
                return new int[] {map.get(target-nums[i]),i};
            }
            map.put(nums[i], i);
            //map.put(2, 0);
//            map.put(15, 1);
//            map.put(11, 2);
        }
        return null;

    }

    public static void main(String[] args) {
        TwoNumSum twoNumSum = new TwoNumSum();
        int[] nums = {2,15,11,7};
        int tar = 9;
        final int[] ints = twoNumSum.twoSum(nums, tar);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }
}
